﻿ 
using SHKF.B2C.MeiTuanPeiSong.Model.Entity.Order;
using SHKF.B2C.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
　

namespace SHKF.B2C.MeiTuanPeiSong.BLL
{
    /// <summary>
    /// 创建配送定单
    /// </summary>
    public class OrderContentBLL: LMBLL
    {　
        public OrderContentBLL(SqlSugarClient db):base(db)
        {
            
        }

        public OrderContentEntity Get(string shopID, string orderID)
        {
            OrderContentEntity order =  this.db
                .Queryable<OrderContentEntity>()
                .Where(oo => oo.shop_id == shopID && oo.order_id == orderID)
                .First();

            if (order != null)
            {
                order.goods_detail = new Model.goods_detail(); 
                order.goods_detail.goods =
                this.db
                .Queryable<OrderDetailEntity>()
                .Where(oo => oo.FKSN == order.SN)
                .ToList();
            }
            
            return order;
        }

        public long Save(OrderContentEntity orderContent)
        {
            try
            {
                OrderContentEntity old = this.db
               .Queryable<OrderContentEntity>()
               .Where(oo => oo.mt_peisong_id == orderContent.mt_peisong_id && oo.delivery_id == orderContent.delivery_id)
               .First();

                long SN = 0;
                if (old != null)
                {
                    SN = old.SN;
                    orderContent.SN = old.SN;
                    this.db
                        .Updateable<OrderContentEntity>(orderContent) 
                        .ExecuteCommand();
                }
                else
                { 
                    SN  = this.db
                       .Insertable<OrderContentEntity>(orderContent)
                       .ExecuteReturnBigIdentity();
                }

                return SN;
            }
            catch (Exception ex)
            { 
                throw ex;
            }
 
        }

        public long AddDetail(OrderDetailEntity orderDetail)
        { 
            long SN = this.db
                   .Insertable<OrderDetailEntity>(orderDetail)
                   .ExecuteReturnBigIdentity();

            return SN;
        }

        public int UpdateStatus(OrderContentEntity orderContent)
        {
            OrderContentEntity old = this.db
               .Queryable<OrderContentEntity>()
               .Where(oo => oo.mt_peisong_id == orderContent.mt_peisong_id && oo.delivery_id == orderContent.delivery_id)
               .First();

            int iResult = 0;
            if (old != null)
            { 
                orderContent.SN = old.SN;
                iResult = this.db
                    .Updateable<OrderContentEntity>(orderContent) 
                    .UpdateColumns(a=> new { a.status, 
                        a.delivery_id,
                        a.order_id, 
                        a.mt_peisong_id, 
                        a.courier_name, 
                        a.courier_phone, 
                        a.cancel_reason_id,
                        a.cancel_reason,
                        a.predict_delivery_time
                    })
                    .Where(oo=>oo.SN == old.SN)
                    .ExecuteCommand();
            }

            return iResult;
        }

        public int UpdateCancel(OrderContentEntity orderContent)
        {
            OrderContentEntity old = this.db
               .Queryable<OrderContentEntity>()
               .Where(oo => oo.mt_peisong_id == orderContent.mt_peisong_id && oo.delivery_id == orderContent.delivery_id)
               .First();

            int iResult = 0;
            if (old != null)
            {
                orderContent.SN = old.SN;
                iResult = this.db
                    .Updateable<OrderContentEntity>(orderContent) 
                    .UpdateColumns(a => new {
                        a.status,
                        a.delivery_id, 
                        a.mt_peisong_id,
                        a.cancel_reason_id,
                        a.cancel_reason
                    })
                    .Where(oo => oo.SN == old.SN)
                    .ExecuteCommand();
            }

            return iResult;
        }

        /// <summary>
        /// 更新异常
        /// </summary>
        /// <param name="orderContent"></param>
        /// <returns></returns>
        public int UpdateException(OrderContentEntity orderContent)
        {
            OrderContentEntity old = this.db
                .Queryable<OrderContentEntity>()
                .Where(oo => oo.mt_peisong_id == orderContent.mt_peisong_id && oo.delivery_id == orderContent.delivery_id)
                .First();

            int iResult = 0;
            if (old != null)
            {
                orderContent.SN = old.SN;
                iResult = this.db
                    .Updateable<OrderContentEntity>(orderContent) 
                    .UpdateColumns(a => new {
                        a.delivery_id,
                        a.mt_peisong_id,
                        a.order_id,

                        a.exception_id,
                        a.exception_code,
                        a.exception_descr,
                        
                        a.exception_time,a.courier_name,a.courier_phone
                    })
                    .Where(oo => oo.SN == old.SN)
                    .ExecuteCommand();
            }

            return iResult;
        }

        /// <summary>
        /// 更新配送码
        /// </summary>
        /// <param name="shopID"></param>
        /// <param name="order_id"></param>
        /// <param name="delivery_qr_code"></param>
        /// <returns></returns>
        public int UpdateDeliveryCode(string shopID, string order_id, string delivery_qr_code)
        {
            OrderContentEntity old = this.db
              .Queryable<OrderContentEntity>()
              .Where(oo => oo.shop_id == shopID && oo.order_id == order_id)
              .First();

            int iResult = 0;
            if (old != null)
            {
                old.delivery_qr_code = delivery_qr_code;
                 iResult = this.db
                    .Updateable<OrderContentEntity>(old) 
                    .UpdateColumns(a => new { a.delivery_qr_code })
                    .Where(oo => oo.SN == old.SN)
                    .ExecuteCommand();
            }

            return iResult;
        }
  
        /// <summary>
        /// 获取配送单号与顺序号
        /// </summary>
        /// <param name="shopID"></param>
        /// <param name="strID"></param>
        /// <returns></returns>
        public int GetOrderID(string shopID, out string strID)
        {
            int iSeq = 1;
            strID = "";
            try
            {
                string now = DateTime.Now.ToString("yyyy-MM-dd");
                this.db.BeginTran();
                 
                OrderSeqEntity old = this.db
                    .Queryable<OrderSeqEntity>()
                    .Where(oo => oo.ShopID == shopID && oo.CreateDate == now)
                    .First();

                if (old != null)
                {
                    ++old.Seq;
                    iSeq = old.Seq;
                    this.db
                        .Updateable<OrderSeqEntity>(old)
                        .ExecuteCommand();
                }
                else
                {
                    old = new OrderSeqEntity();
                    old.Seq = iSeq;
                    old.CreateDate = now;
                    old.ShopID = shopID;
                    this.db
                    .Insertable<OrderSeqEntity>(old)
                    .ExecuteCommand();
                }

                this.db.CommitTran();

                if (shopID.Length > 20)
                {
                    shopID = shopID.Substring(0, 20);
                }
                strID = shopID + now.Replace("-", "") + iSeq.ToString().PadLeft(4, '0');
            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }

            return iSeq;
        }

        public List<OrderContentEntity> Query(string shopID, string order_id, int status, DateTime startDate, DateTime endDate)
        {
            var cmd = this.db.Queryable<OrderContentEntity>();

            if (!string.IsNullOrEmpty(order_id))
            {
                cmd.Where(oo => oo.order_id.Contains(order_id));
            }
             
            if (!string.IsNullOrEmpty(shopID))
            {
                cmd.Where(oo => oo.shop_id == shopID);
            }

            if (status >= 0)
            {
                cmd.Where(oo => oo.status == status);
            }

            if (startDate.Year != 1900 && startDate.Year != 1)
            {
                long lStartDate = DateTimeUtil.DateTimeToTimeStamp(startDate);
                cmd.Where(oo => oo.timestamp >= lStartDate);
            }

            if (endDate.Year != 1900 && endDate.Year != 1)
            {
                long lEndDate = DateTimeUtil.DateTimeToTimeStamp(endDate); 
                cmd.Where(oo => oo.timestamp <= lEndDate); 
            }

            return cmd.ToList();
        }

        public List<OrderDetailEntity> QueryDetail(long SN)
        {
            var cmd = this.db
                          .Queryable<OrderDetailEntity>();
            if (SN > 0)
            {
                cmd.Where(oo => oo.FKSN == SN);
            }

            return cmd.ToList();

        }
    }
}
